define(['jquery', 'bootstrap', 'form', 'table', 'ztree'], function ($, undefined, Form, Table, ztree) {
        var Controller = {
            index: function () {
                var table = $("#table");
                // 初始化表格参数配置
                Table.api.init({
                    extend: {
                        index_url: '/auth/group/index' + location.search,
                        add_url: '/auth/group/add',
                        edit_url: '/auth/group/edit',
                        del_url: '/auth/group/del',
                        table: 'auth_group',
                    }
                });

                $(table).bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    pagination: false,
                    escape: false,
                    columns: [
                        {checkbox: true,},
                        {field: 'id', title: 'ID',},
                        {field: 'title', title: '组名称', align: 'left'},
                        {field: 'rules', title: '规则'},
                        {
                            field: 'operate',
                            title: '操作',
                            table: table,
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                if (Config.current_group_ids.indexOf(parseInt(row.id)) >= 0) {
                                    return '';
                                }
                                return Table.api.formatter.operate.call(this, value, row, index);
                            }
                        }],
                })
                // 为表格绑定事件
                Table.api.bindevent(table);
            },
            add: function () {
                Controller.api.bindevent();
            },
            edit: function () {
                Controller.api.bindevent();
            },

            events: {
                z_tree: function () {
                    //配置
                    var setting = {
                        check: {
                            enable: true,
                        },
                        data: {
                            simpleData: {
                                enable: true,
                                idKey: "id",
                                pIdKey: "pid",
                                rootPId: 0
                            },
                            key: {
                                name: "title"
                            }
                        },
                        view: {
                            expandSpeed: 100,
                            selectedMulti: true,
                            showLine: true, // 是否显示节点间的连线
                            fontCss: {},
                        },
                        callback: {
                            onCheck: function () {
                                Controller.events.fill_value()
                            },
                            onNodeCreated: function () {
                                Controller.events.fill_value()
                            }
                        }
                    };
                    $(document).on('change', "#c-pid", function () {
                        var current_pid = $(this).data('pid');
                        var current_id = $(this).data('id');
                        var pid = $(this).val();
                        //如果相等则表示 选择自己为父级 这是不允许的
                        if (pid == current_id) {
                            $("option[value='" + current_pid + "']", this).prop("selected", true).change();
                            Toastr.error('不允许以自己为父级');
                            return false;
                        }
                        axios({
                            url: '/auth/group/ruleList',
                            data: {id: current_id, pid: pid},
                            method: "post",
                        }).then(function (response) {
                            data = response.data;
                            if (data.code == 1) {
                                $.fn.zTree.destroy("z-tree");
                                $.fn.zTree.init($("#z-tree"), setting, data.data);
                            } else {
                                parent.Toastr.error(data.msg);
                                $.fn.zTree.destroy("z-tree");
                            }
                        }).catch(function (response) {
                            $.fn.zTree.destroy("z-tree");
                        });

                    })
                    $("#c-pid").trigger('change');

                    $(".check").change(function () {
                        var type = $(this).data('type');
                        var treeObj = $.fn.zTree.getZTreeObj("z-tree");
                        if (type == 'check') {
                            if ($(this).is(':checked')) {
                                treeObj.checkAllNodes(true);
                                Controller.events.fill_value();
                            } else {
                                treeObj.checkAllNodes(false);
                                Controller.events.fill_value();
                            }

                        } else {
                            if ($(this).is(':checked')) {
                                treeObj.expandAll(true);
                            } else {
                                treeObj.expandAll(false);
                            }
                        }
                    })
                },
                fill_value: function () {
                    var treeObj = $.fn.zTree.getZTreeObj("z-tree");
                    var nodes = treeObj.getCheckedNodes(true);
                    var rule = [];
                    $.each(nodes, function (k, v) {
                        rule.push(v.id);
                    })
                    $("#rules").val(rule);
                }
            },
            api: {
                bindevent: function () {
                    var form = $('form[role="form"]');
                    Form.api.bindevent(form);
                    Controller.events.z_tree()
                }
            }

        };
        return Controller;
    }
);
